package com.hdax.dmw.order.mapstruct;

import com.hdax.dmw.entity.order.Order;
import com.hdax.dmw.order.dtos.OrderListDto;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.Mappings;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Mapper(componentModel = "spring")
public abstract class OrderListDtoMapper {


    protected String getSellTime(LocalDateTime createdTime){
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.format(createdTime);
    }
    //15分钟内完成支付
    protected String getEndTime(LocalDateTime createdTime){
        LocalDateTime endTime = createdTime.plusMinutes(15L);
        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        return dateTimeFormatter.format(endTime);
    }

    @Mappings({
            @Mapping(target = "sellTime",expression = "java(getSellTime(source.getCreatedTime()))"),
            @Mapping(target = "endTime",expression = "java(getEndTime(source.getCreatedTime()))"),
            @Mapping(source = "totalCount",target = "num")
    })
    public abstract OrderListDto toTarget(Order source);
    public abstract List<OrderListDto> toTargetList(List<Order> sources);
}
